<?php
namespace app\finance\controller;

use think\Db;
use think\Controller;
use Godok\Org\Auth;
use think\Config;

class Index extends Base
{
    /**
     * 控制台
     */
    // http://127.0.0.1/auth/Index/index
    public function index()
    {
        $groupids = \Godok\Org\Auth::user('groupids');
        $data = ['groupname'=>'游客'];
        if( !empty($groupids) ) {
            $group = Db::name( Config::get('auth.table_group') )->where(['id' => $groupids[0]])->find();
            if ( $group ) {
                $data['groupname'] = $group['title'];
            }
        }
        $data['menu'] = $this->menu2Html( \Godok\Org\Auth::menu("ismenu=1 AND status=1") );

		return $this->fetch('index',$data);
    }

    
    /**
     * 首页
     */
    public function home(){
        $iw=Request()->param();

        $start_time=isset($iw['start_time'])?strtotime($iw['start_time']):'';
        $end_time=isset($iw['end_time'])?strtotime($iw['end_time']):'';

        if($end_time){
            $end_time=getOneDay($end_time);
            $end_time=$end_time['et'];
        }else{
            $end_time=getOneDay();
            $end_time=$end_time['et'];
        }

        if ($start_time) {
            $start_time=getOneDay($start_time);
            $start_time=$start_time['st'];
        }else{
            $start_time=getDayRegion($end_time,7);
            $start_time=$start_time['st'];
        }
        
        $iw['start_time']=date('Y-m-d H:i:s',$start_time);
        $iw['end_time']=date('Y-m-d H:i:s',$end_time);

        if ($iw['selt']!=='0') {
            $iw['selt']=$iw['selt']?:8;
        }
        $data['w']=$iw;

        $dayMap=getDayMapSE($start_time,$end_time);
        //订单
        $_order = Db::field('order_id,pay_money,pay_time,order_status')->name('order')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $order_finish = []; $order_nfinish = [];
        foreach ($_order as $k => $v) {
            if ($v['order_status']==100 || $v['order_status']==10) {
                $order_finish[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['pay_money'];
            }else{
                $order_nfinish[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['pay_money'];
            }
        }
        //补价
        $_change = Db::field('order_id,price,pay_time')->name('order_change')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $changeent = [];$changeout = [];
        foreach ($_change as $k => $v) {
            if ($v['price']<0) {
                $changeout[date('Y-m-d',strtotime($v['pay_time']))]['money'] += -1*$v['price'];
            }else{
                $changeent[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['price'];
            }
            
        }
        
        //退货
        $_refundgoods = Db::field('order_id,(return_money*rg_num) as money, pay_time')->name('order_goods_return')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $refundgoods=[];
        foreach ($_refundgoods as $k => $v) {
            $refundgoods[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['money'];
        }
        //采购支款
        $_appropriation = Db::field('create_time,type,money')->name('appropriation')->where("create_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $appropriation=[];
        foreach ($_appropriation as $k => $v) {
            if ($v['type']) { //收到退款
                $appropriation[date('Y-m-d',strtotime($v['create_time']))]['money'] -= $v['money'];
            }else{
                $appropriation[date('Y-m-d',strtotime($v['create_time']))]['money'] += $v['money'];
            }
            
        }

        $arr = []; $ent_finish_total=0;$ent_nfinish_total=0;$out_pur_total=0;$out_refund_total=0;
        foreach ($dayMap as $k => $v) {
            $_date = date('Y-m-d',$k);
            $arr[$_date]['ent_finish'] +=  $order_finish[$_date]['money'];
            $arr[$_date]['ent_finish'] +=  $changeent[$_date]['money'];

            $arr[$_date]['ent_nfinish'] +=  $order_nfinish[$_date]['money'];

            $arr[$_date]['out_pur'] += $appropriation[$_date]['money'];

            $arr[$_date]['out_refund'] += $refundgoods[$_date]['money'];
            $arr[$_date]['out_refund'] += $changeout[$_date]['money'];

            $ent_finish_total += $arr[$_date]['ent_finish'];
            $ent_nfinish_total += $arr[$_date]['ent_nfinish'];
            $out_pur_total += $arr[$_date]['out_pur'];
            $out_refund_total += $arr[$_date]['out_refund'];
        }
        $data['list'] =$arr;
        $data['ent_finish_total'] = $ent_finish_total;
        $data['ent_nfinish_total'] = $ent_nfinish_total;
        $data['out_pur_total'] = $out_pur_total;
        $data['out_refund_total'] = $out_refund_total;

        foreach ($dayMap as $k => $v) {
            $data['dayMap'] .= '"'.date('Y-m-d',$k).'",';
        }
        $data['dayMap'] = '['.rtrim($data['dayMap'],',').']'; // 按天显示
        $E=''; $O='';
        foreach ($arr as $k => $v) {
            $E .= $v['ent_finish']+$v['ent_nfinish'].',';
            $O .= $v['out_pur']+$v['out_refund'].',';
        }
        $data['E'] = '['.rtrim($E,',').']';
        $data['O'] = '['.rtrim($O,',').']';
        return $this->fetch('',$data);
    }

    /**
     * 格式化菜单
     */
    private  function menu2Html($list,$level = 0)
    {
        $menu = '';
        $menulevel = ['first','second','third'];
        if ( is_array($list) ) {
            if( $level > 2 ) {
                $level = 2;
            }
            $menu .= $level > 0  ? '<ul class="nav nav-'.$menulevel[$level].'-level">' : '';
            foreach ( $list as $li ) {
                $menu .= '<li>';
                if( empty($li['module']) || empty($li['controller']) || '*' == $li['module'] || '*' == $li['controller'] ) {
                    $menu .= '<a href="javascript:void(0)" >';
                } else {
                    $act = explode(',',$li['action']);
                    $href = \think\Request::instance()->root().'/'. $li['module'] . '/' . $li['controller'] . '/' . $act[0];
                    if( !empty($li['condition']) ) {
                        $href .= '?' . $li['condition'];
                    }
                    $menu .= '<a href="' . $href . '" class="J_menuItem">';
                }
    
                if ( !empty($li['icon']) ) {
                    $menu .= '<i class="' . $li['icon'] . '"></i> ';
                }
                $menu .= '<span class="nav-label">' . $li['title'] . '</span>';
                if ( isset($li['children']) ) {
                    $children = $this->menu2Html($li['children'], $level + 1 );
                    $menu .= ' <span class="fa arrow"></span>';
                } else {
                    $children = '';
                }
                $menu .= '</a>' . $children;
                $menu .= '</li>';
            }
    
            $menu .= $level > 0 ? '</ul>' : '';
            return $menu;
        } else {
            return '';
        }
    }
}
